package com.maxjay.system.dao.hibernate;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.maxjay.system.dao.BaseDao;

@SuppressWarnings({"rawtypes", "unchecked"})
public class BaseDaoHibernate extends HibernateDaoSupport implements BaseDao {

	public void delete(Object entity) {
		this.getHibernateTemplate().delete(entity);
	}
	
	public void delete( Class objClass, Serializable objId) {
		this.getHibernateTemplate().delete(this.getHibernateTemplate().load(objClass, objId));
	}

	public void save(Object entity) {
		this.getHibernateTemplate().save(entity);
	}

	public void update(Object entity) {
		this.getHibernateTemplate().update(entity);
	}
	
	public Object get(Class clazz, Serializable id) {
		return this.getHibernateTemplate().get(clazz, id);
	}

	public Object load(Class clazz, Serializable id) {
		return this.getHibernateTemplate().load(clazz, id);
	}

	public void saveOrUpdate(Object entity) {
		this.getHibernateTemplate().saveOrUpdate(entity);
	}

	public List findByHql(String hql, Object[] args) {
		return this.getHibernateTemplate().find(hql, args);
	}

	public List findByHql(String hql, Object[] args, boolean useCache) {
		// 设置是否使用缓存策略
		this.getHibernateTemplate().setCacheQueries(useCache);
		return this.getHibernateTemplate().find(hql, args);
	}
	
	public List findByCriteria(DetachedCriteria dc) {
		return this.getHibernateTemplate().findByCriteria(dc);
	}

	public Iterator iterateByHql(String hql, Object[] args) {
		return this.getHibernateTemplate().iterate(hql, args);
	}

	public List findBySql(final String sql, Object[] args) {
		//TODO: 根据sql查询
		this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				return null;
			}
		});
		return null;
	}
	
	public void batchSave(final List objList) {
		this.getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				for (int i = 0; i < objList.size(); i++) {
					session.save(objList.get(i));
					if (i % 20 == 0) {
						session.flush();
						session.clear();
					}
				}
				return null;
			}
		});
	}
	
	/**
	 * 获得分页数据对象
	 * @param hql
	 * @param pageStart
	 * @param maxResult
	 * @return
	 */
	public List getPagingObjects(final String hql, final int pageStart, final int maxResult) {
		HibernateCallback hc = new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				query.setMaxResults(maxResult);
				query.setFirstResult(pageStart);
				List result = query.list();
				return result;
			}
		};
		return getHibernateTemplate().executeFind(hc);
	}
	
	
	/**
	 * 获得分页数据对象
	 * @param hql
	 * @param pageStart
	 * @param maxResult
	 * @return
	 */
	public List getPagingObjects(final DetachedCriteria dc, final int pageStart, final int maxResult) {
		HibernateCallback hc = new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Criteria criteria = dc.getExecutableCriteria(session);
				criteria.setMaxResults(maxResult);
				criteria.setFirstResult(pageStart);
				List result = criteria.list();
				return result;
			}
		};
		return getHibernateTemplate().executeFind(hc);
	}
	
	public void batchInsert(String sql, List params){
		
	}

	public void bulkUpdate(String hql, Object[] values) {
		this.getHibernateTemplate().bulkUpdate(hql, values);
	}

	// public void executeSql(String sql) {
	// this.getJdbcTemplate().execute(sql);
	// }

	public void updateSql(String sql, Object[] values) {
		//TODO: 批量更新
	}


}
